package com.tyj.movie.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tyj.movie.entity.MovieActor;
import com.tyj.movie.service.MovieActorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author tyj
 * @since 2021-06-28
 */
@Controller
@RequestMapping("/movie/movie-actor")
public class MovieActorController {

    @Autowired
    MovieActorService movieActorService;

    /**
     * 增加或修改
     * @param movieActor
     * @return
     */
    @PostMapping("/add")
    public String addActor(MovieActor movieActor){
        //判断是否是添加
        if (movieActor.getId()==null || movieActor.getId().equals("")){
            //添加
            movieActor.setCreateDate(new Date());
            movieActorService.save(movieActor);
        }else {
            if (movieActor.getPictureUrl()==""){
                movieActor.setPictureUrl(null);
            }
            //修改
            movieActorService.updateById(movieActor);
        }

        return "redirect:/movie/movie-actor/actorTables/1";
    }

    @GetMapping("/actorTables/{pageNum}")
    public ModelAndView actorPagingQuery(@PathVariable("pageNum") Long pageNum){
        Page<MovieActor> page = movieActorService.actorPagingQuery(pageNum);
        ModelAndView modelAndView=new ModelAndView();
        modelAndView.addObject("page",page);
        modelAndView.setViewName("actorTables");
        return modelAndView;
    }

    @GetMapping("/delete/{id}")
    public String  delete(@PathVariable("id") Long id ){
        movieActorService.removeById(id);
        return "redirect:/movie/movie-actor/actorTables/1";
    }

    /**
     * 转发到修改页面
     * @param id
     * @return
     */
    @GetMapping("/edit/{id}")
    public ModelAndView  editToPage(@PathVariable("id") Long id ){
        ModelAndView modelAndView=new ModelAndView();
        MovieActor movieActor = movieActorService.getById(id);
        modelAndView.addObject("movieActor",movieActor);
        modelAndView.setViewName("/actorAddOrEditor");
        return modelAndView;
    }

}

